package com.ruoyi.order.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.order.domain.OrderBy;
import com.ruoyi.order.service.IOrderByService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2023-10-12
 */
@RestController
@RequestMapping("/orderBy")
public class OrderByController extends BaseController
{
    @Autowired
    private IOrderByService orderByService;

    /**
     * 查询订单列表
     */
    @RequiresPermissions("order:orderBy:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderBy orderBy)
    {
        startPage();
        List<OrderBy> list = orderByService.selectOrderByList(orderBy);
        return getDataTable(list);
    }


    /**
     * 查询订单列表
     */
    @RequiresPermissions("order:orderBy:list")
    @GetMapping("/getOrderDetail/{shopsId}")
    public List<OrderBy> getOrderDetail(@PathVariable("shopsId") Long shopsId)
    {
        //startPage();
        List<OrderBy> orderBy = orderByService.getOrderDetail(shopsId);
        return orderBy;
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("order:orderBy:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderBy orderBy)
    {
        List<OrderBy> list = orderByService.selectOrderByList(orderBy);
        ExcelUtil<OrderBy> util = new ExcelUtil<OrderBy>(OrderBy.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @RequiresPermissions("order:orderBy:query")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(orderByService.selectOrderByByOrderId(orderId));
    }

    /**
     * 获取订单详细信息
     */
    @RequiresPermissions("order:orderBy:status")
    @GetMapping(value = "/{status}")
    public List<OrderBy> getStatus(@PathVariable("status") Long status)
    {
        System.out.println("status" + status);
        return  orderByService.selectOrderByByStatus(status);
    }

    /**
     * 根据用户Id获取订单详细信息
     */
    @PostMapping(value = "getOrderByUserId/{userId}")
    public R<List<OrderBy>> getOrderByUserId(@PathVariable("userId") String userId) {
        List<OrderBy> list =orderByService.selectOrderByUserId(Long.valueOf(userId));
        return R.ok(list);
    }

    /**
     * 新增订单
     */
    @RequiresPermissions("order:orderBy:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderBy orderBy)
    {
        return toAjax(orderByService.insertOrderBy(orderBy));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("order:orderBy:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderBy orderBy)
    {
        return toAjax(orderByService.updateOrderBy(orderBy));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("order:orderBy:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(orderByService.deleteOrderByByOrderIds(orderIds));
    }
    @RequiresPermissions("order:orderBy:delete")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderId}")
    public AjaxResult delete(@PathVariable Long orderId)
    {
        return toAjax(orderByService.deleteOrderByByOrderId(orderId));
    }
}
